﻿using Microsoft.Win32;

namespace JoinBoxCurrency
{
    public class FileTypeRegInfo
    {
        /// <summary>  
        /// 扩展名  
        /// </summary>  
        public string ExtendName;
        /// <summary>  
        /// 说明  
        /// </summary>  
        public string Description;
        /// <summary>  
        /// 关联的图标  
        /// </summary>  
        public string IconPath;
        /// <summary>  
        /// 应用程序路径
        /// </summary>  
        public string ProjectPath;
          
        /// <summary>
        /// 启动的exe名称
        /// </summary>
        public string RelationName
        {
            get
            {
                return this.ExtendName.Substring(1, this.ExtendName.Length - 1) + "Project";
            } 
        }

        /// <summary>
        /// 文件类型注册信息
        /// </summary>
        /// <param name="extendName">后缀名,带点</param>
        public FileTypeRegInfo(string extendName)
        {
            this.ExtendName = extendName;
        }

        /// <summary>  
        /// 使文件类型与对应的图标及应用程序关联起来
        /// </summary>          
        public void RegisterFileType()
        { 
            //在windows服务中,如果你不手工删除一次{/计算机\HKEY_CLASSES_ROOT\.vlx}注册表,
            //那么以下代码将会有很奇怪的表现,
            //你通过这里的代码无法删除.vlx注册表项.
            //你添加在SetValue("",relationName)默认项的在程序结束后也不会呈现,但立即GetValue("")却可以获取值..
            //手工删除一次,即可解决所有的问题..

            //计算机\HKEY_CLASSES_ROOT\.vlx 上面写引用的注册程序vlxProject
            var fileTypeKey = Registry.ClassesRoot.CreateSubKey(this.ExtendName);
            fileTypeKey.SetValue("", RelationName);//默认位置就是"" 
            fileTypeKey.Close();

            //计算机\HKEY_CLASSES_ROOT\vlxProject 注册了一个打开程序,这个程序内写打开的信息
            RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(RelationName);
            relationKey.SetValue("", this.Description);
            RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
            iconKey.SetValue("", this.IconPath.Replace("\\", "/"));
            RegistryKey shellKey = relationKey.CreateSubKey("Shell");
            RegistryKey openKey = shellKey.CreateSubKey("Open");
            RegistryKey commandKey = openKey.CreateSubKey("Command");
            commandKey.SetValue("", $"\"{this.ProjectPath}\" \"%1\""); // " %1"表示将被双击的文件的路径传给目标应用程序 
            relationKey.Close();
        }
         

        /// <summary>
        /// 删除注册表项-后缀名
        /// </summary>
        public void DeleteRegExtend()
        {
            var fileTypeKey = Registry.ClassesRoot.OpenSubKey(this.ExtendName, true);
            if (fileTypeKey != null)
            {
                fileTypeKey.DeleteValue("");
                fileTypeKey.Close();
            }
        }

        /// <summary>
        /// 删除注册表项-转发器程序
        /// </summary>
        public void DeleteRegRelation()
        {
            var fileTypeKey = Registry.ClassesRoot.OpenSubKey(RelationName);
            if (fileTypeKey != null)
            {
                Registry.ClassesRoot.DeleteSubKeyTree(RelationName);
            }
        }

        /// <summary>  
        /// 更新指定文件类型关联信息  
        /// </summary>      
        public bool UpdateFileTypeRegInfo()
        {
            if (!FileTypeRegistered())
            {
                return false;
            }

            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(RelationName, true);
            relationKey.SetValue("", this.Description);
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
            iconKey.SetValue("", this.IconPath);
            RegistryKey shellKey = relationKey.OpenSubKey("Shell");
            RegistryKey openKey = shellKey.OpenSubKey("Open");
            RegistryKey commandKey = openKey.OpenSubKey("Command", true);
            commandKey.SetValue("", $"\"{this.ProjectPath}\" \"%1\""); // " %1"表示将被双击的文件的路径传给目标应用程序 
            relationKey.Close();
            return true;
        }


        /// <summary>  
        /// 获取指定文件类型关联信息  
        /// </summary>          
        public FileTypeRegInfo GetFileTypeRegInfo()
        {
            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(RelationName);
            this.Description = relationKey.GetValue("").ToString();
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
            this.IconPath = iconKey.GetValue("").ToString();
            RegistryKey shellKey = relationKey.OpenSubKey("Shell");
            RegistryKey openKey = shellKey.OpenSubKey("Open");
            RegistryKey commandKey = openKey.OpenSubKey("Command");
            string temp = commandKey.GetValue("").ToString();
            this.ProjectPath = temp.Substring(0, temp.Length - 3);
            return this;
        }

        /// <summary>  
        /// 指定文件类型是否已经注册  
        /// </summary>          
        public bool FileTypeRegistered()
        {
            RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(ExtendName);
            if (softwareKey != null)
            {
                return true;
            }
            return false;
        }
    }
}
